<?php
    $xtpl_list  = new XTemplate("view/product/list.htm");
    
    $title      = "Thông tin sản phẩm";
    $searchBy   = $_POST['searchby'];
    $offset     = $_GET['p'];
    $limit      = 10;
    $offset     = (isset($offset) && ($offset > 0)) ? $offset : 0;
    
    $keyword        = $_POST['keyword'];
   	$keyword		= str_replace(" ", "%", $keyword);
   	$keyword 		= "LIKE '%" . $keyword . "%'";
    switch($searchBy){
        case    1   :   $condition  = "milkname $keyword"; break;
        case    2   :   $condition  = "provider $keyword"; break;
        default     :   $condition  = "milkname $keyword"; break;
    }    
    
    if(isset($_POST['submit'])){
        $totalRecords   = 0;
        if($_POST['keyword']!=NULL){
            $query          = "SELECT * FROM product WHERE $condition";
            $re             = mysql_query($query);
            if(isset($re)){
                while($row = mysql_fetch_array($re)){
                    $xtpl_list  -> assign('image', $row['image']);
                    $xtpl_list  -> assign('milkname', $row['milkname']);
                    $xtpl_list  -> assign('provider', $row['provider']);
                    $xtpl_list  -> assign('cost', $row['cost']);
                    $xtpl_list  -> assign('number', $row['number']);
                    $xtpl_list  -> assign('id3', $row['id_p']);
                    $xtpl_list  -> assign('id1', $row['id_p']);
                    $xtpl_list  -> assign('id2', $row['id_p']);
                    $xtpl_list  -> insert_loop('LIST.TABLES', array('LIST'=>$row));
                    $totalRecords++;
                }
            }
        }
        else{
            $error = "Bạn chưa điền từ khóa";
            $xtpl_list  -> assign('error', $error);
        }
    }
    else{
        $query          = "SELECT count(*) AS totalRecords FROM product";
        $re             = mysql_query($query);
        $rows           = mysql_fetch_assoc($re);
        $totalRecords   = $rows['totalRecords'];
        $pages      = pagging($totalRecords, $offset, $limit, '?page=product');
        $sql        = "SELECT * FROM product ORDER BY id_p DESC LIMIT " . $offset . ", " . $limit . "";
        $result     = mysql_query($sql);
        while($row = mysql_fetch_array($result)){
            $xtpl_list  -> assign('image', $row['image']);
            $xtpl_list  -> assign('name', $name);
            $xtpl_list  -> assign('milkname', $row['milkname']);
            $xtpl_list  -> assign('provider', $row['provider']);
            $xtpl_list  -> assign('cost', $row['cost']);
            $xtpl_list  -> assign('number', $row['number']);
            $xtpl_list  -> assign('id3', $row['id_p']);
            $xtpl_list  -> assign('id1', $row['id_p']);
            $xtpl_list  -> assign('id2', $row['id_p']);
            $xtpl_list  -> insert_loop('LIST.TABLES', array('LIST'=>$row));
        }
    }
    
    if(isset($_POST['add'])){
        header("location:index.php?page=product&module=add");
    }
    
    if(isset($_POST['delete'])){
        if(is_array($_POST['check'])){
            $list_id    = join($_POST['check'], ',');
            $sql    = "DELETE FROM product WHERE id_p IN (" . $list_id . ")";
            $result = mysql_query($sql);
            header("location:index.php?page=product");
        }
    }

    $xtpl_list  -> assign('total', $totalRecords);    
    $xtpl_list  -> assign('pages', $pages);
    $xtpl_list  -> parse("LIST");
    $content    = $xtpl_list -> text("LIST");
?>